package utils

import (
	"XiangMu/define"
	"XiangMu/models"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/jinzhu/copier"
	"github.com/nguyenthenguyen/docx"
	"log"
	_ "log"
	"path/filepath"
	"strconv"
	"strings"
)

type Contract struct {
	Id            int    `json:"id"`
	Account       string `json:"account"`
	Organization  string `json:"organization"`
	Name          string `json:"Name" comment:"姓名"`
	Sex           string `json:"sex"`
	Nation        string `json:"nation"`
	Birthday      string `json:"birthday"`
	Origin        string `json:"origin"`
	Occupation    string `json:"occupation"`
	Education     string `json:"education"`
	IdentityId    string `json:"identity_id"`
	Address       string `json:"address"`
	Phone         string `json:"phone"`
	Resume        string `json:"resume"`
	PoliticalTime string `json:"political_time"`
	Domicile      string `json:"domicile"`
}

// FillTemplate 填充模版
func (p *Contract) FillTemplate(templatePath string, symbol string, replacements map[string]string) (url string, err error) {
	// 打开模板
	r, err := docx.ReadDocxFile(templatePath)
	if err != nil {
		return "", errors.New("打开模板失败")
	}
	defer r.Close()
	doc := r.Editable()

	for placeholder, value := range replacements {
		if err = doc.Replace(placeholder, value, -1); err != nil {
			log.Printf("替换占位符 %s 失败: %v", placeholder, err)
			// 可以根据需求决定是否返回错误
			// return err
			return "", errors.New("替换占位失败")
		}

	}
	// 生成新的文件路径
	newFileName := symbol + p.Account + define.EXCEL_WORD
	url = symbol + p.Account + define.EXCEL_PDF
	newFilePath := filepath.Join(newFileName)
	// 保存修改后的文档
	err = doc.WriteToFile(newFilePath)
	if err != nil {
		return "", errors.New("保存修改后的文档出错")
	}

	return "https://web-aliyuncjh.oss-cn-guangzhou.aliyuncs.com/" + url, nil
}

func WordFill(user models.UserResponse) (string, string) {
	ALiYunOssGet("user.docx")
	contact := &Contract{}
	copier.Copy(&contact, &user)
	contact.Birthday = user.Birthday
	//获取步骤详情
	stepInfo, err := ParseStep(contact.Id)
	if err != nil {
		return "", ""
	}

	politicalTxt := political(stepInfo[2]["群团组织推优情况"], stepInfo[2])
	step100 := step100Text(stepInfo[2])
	step101 := step101Text(stepInfo[2])
	// 替换占位符
	replacements := map[string]string{
		"{1}":         contact.Organization,
		"{2}":         contact.Name,
		"{3}":         contact.Sex,
		"{4}":         contact.Nation,
		"{5}":         contact.Birthday,
		"{6}":         contact.Origin,
		"{7}":         contact.Occupation,
		"{10}":        contact.Education,
		"{8}":         contact.IdentityId,
		"{9}":         contact.Address,
		"{11}":        contact.Phone,
		"{78}":        contact.PoliticalTime,
		"{political}": politicalTxt,
		//群团组织推优落款时间
		"{12}":     stepInfo[0]["入党申请时间"],
		"{13}":     stepInfo[0]["入党申请书接收人(单位、职务、签名)"],
		"{14}":     stepInfo[1]["支部谈话时间"],
		"{15}":     stepInfo[1]["谈话人(单位、职务、签名)"],
		"{16}":     stepInfo[2]["党员推荐情况"],
		"{17}":     stepInfo[2]["群团组织推优情况"],
		"{18}":     stepInfo[2]["支部确定入党积极分子时间"],
		"{100}":    step100,
		"{101}":    step101,
		"{19}":     stepInfo[3]["党委备案时间"],
		"{20}":     stepInfo[4]["培养联系人1"],
		"{21}":     stepInfo[4]["单位及职位1"],
		"{22}":     stepInfo[4]["培养联系人2"],
		"{23}":     stepInfo[4]["单位及职位2"],
		"{24}":     stepInfo[5]["书面思想汇报时间1"],
		"{25}":     stepInfo[5]["书面思想汇报时间2"],
		"{26}":     stepInfo[5]["书面思想汇报时间3"],
		"{27}":     stepInfo[5]["书面思想汇报时间4"],
		"{28}":     stepInfo[5]["支部考察时间1"],
		"{29}":     stepInfo[5]["支部考察时间2"],
		"{30}":     stepInfo[6]["支部确定发展对象人选时间"],
		"{31}":     stepInfo[6]["公示起止时间"],
		"{32}":     stepInfo[6]["党总支审议时间"],
		"{33}":     stepInfo[7]["党委备案同意时间"],
		"{34}":     stepInfo[8]["入党介绍人1"],
		"{35}":     stepInfo[8]["单位及职务1"],
		"{36}":     stepInfo[8]["入党介绍人2"],
		"{37}":     stepInfo[8]["单位及职务2"],
		"{38}":     stepInfo[9]["政审是否合格"],
		"{39}":     stepInfo[9]["政审人(单位、职务、签名)"],
		"{40}":     stepInfo[10]["参加发展对象集中培训起止时间"],
		"{41}":     stepInfo[10]["培训成绩"],
		"{42}":     stepInfo[11]["支部审查时间"],
		"{43}":     stepInfo[12]["党委预审时间"],
		"{44}":     stepInfo[13]["入党志愿书编号"],
		"{45}":     stepInfo[14]["支部大会会议时间"],
		"{46}":     stepInfo[14]["有表决权党员人数"],
		"{47}":     stepInfo[14]["应到人数"],
		"{48}":     stepInfo[14]["实到人数"],
		"{49}":     stepInfo[14]["赞成票数"],
		"{50}":     stepInfo[14]["反对票数"],
		"{51}":     stepInfo[14]["弃权票数"],
		"{52}":     stepInfo[15]["党委派人谈话时间"],
		"{53}":     stepInfo[15]["谈话人(单位、职务、签名)"],
		"{54}":     stepInfo[16]["党委审批时间"],
		"{55}":     stepInfo[16]["党委委员人数"],
		"{56}":     stepInfo[16]["实到人数"],
		"{57}":     stepInfo[16]["同意人数"],
		"{58}":     stepInfo[16]["预备期起止时间"],
		"{59}":     stepInfo[17]["向上级党委组织部门备案时间"],
		"{60}":     stepInfo[18]["编入党支部名称"],
		"{61}":     stepInfo[19]["入党宣誓时间"],
		"{62}":     stepInfo[21]["申请转正时间"],
		"{63}":     stepInfo[21]["转正公示起止时间"],
		"{64}":     stepInfo[22]["支部大会会议时间"],
		"{65}":     stepInfo[22]["有表决权党员人数"],
		"{66}":     stepInfo[22]["应到人数"],
		"{67}":     stepInfo[22]["实到人数"],
		"{68}":     stepInfo[22]["赞成票数"],
		"{69}":     stepInfo[22]["反对票数"],
		"{70}":     stepInfo[22]["弃权票数"],
		"{71}":     stepInfo[22]["会议决议"],
		"{72}":     stepInfo[23]["党委审批时间"],
		"{73}":     stepInfo[23]["党委委员人数"],
		"{74}":     stepInfo[23]["实到人数"],
		"{75}":     stepInfo[23]["同意人数"],
		"{76}":     stepInfo[23]["党龄起算时间"],
		"{77}":     stepInfo[23]["档案整理人(签名)"],
		"{153}":    strings.Split(stepInfo[0]["入党申请书接收人(单位、职务、签名)"], " ")[2],
		"{a1}":     stepInfo[2]["会议记录人"],
		"{a2}":     stepInfo[2]["会议地点"],
		"{a3}":     stepInfo[2]["会议主持人"],
		"{resume}": contact.Resume,
	}

	//检验党支部:
	checkOrg(replacements, contact.Id, 3)
	//积极分子
	url, err := contact.FillTemplate("user.docx", "Active", replacements)
	if err != nil {
		log.Printf("Error filling template: %v", err)
	}
	word := "Active" + user.Account + define.WORD_NAME
	pdf := "Active" + user.Account + define.PDF_NAME
	ConvertDocxToPDF(word)
	ALiYunOssPut(pdf)
	defer DelFile("user.docx")
	defer DelFile(word)
	return pdf, url
}
func WordFill1(user models.UserResponse) (string, string) {
	ALiYunOssGet("user1.docx")
	contact := &Contract{}
	copier.Copy(&contact, &user)
	contact.Birthday = user.Birthday
	//获取步骤详情
	stepInfo, err := ParseStep(contact.Id)
	if err != nil {
		return "", ""
	}
	endTimeAddOne31 := EndAndAddOne(stepInfo[6]["公示起止时间"])
	fmt.Println("endTimeAddOne31=", endTimeAddOne31)
	step80 := fmt.Sprintf("%s，%s，%s族，%s文化,%s", contact.Name, contact.Sex, contact.Nation, contact.Education, contact.Domicile)
	// 替换占位符
	replacements := map[string]string{
		"{1}":   contact.Organization,
		"{2}":   contact.Name,
		"{3}":   contact.Sex,
		"{4}":   contact.Nation,
		"{5}":   contact.Birthday,
		"{6}":   contact.Origin,
		"{7}":   contact.Occupation,
		"{10}":  contact.Education,
		"{8}":   contact.IdentityId,
		"{9}":   contact.Address,
		"{11}":  contact.Phone,
		"{80}":  step80,
		"{12}":  stepInfo[0]["入党申请时间"],
		"{13}":  stepInfo[0]["入党申请书接收人(单位、职务、签名)"],
		"{14}":  stepInfo[1]["支部谈话时间"],
		"{15}":  stepInfo[1]["谈话人(单位、职务、签名)"],
		"{16}":  stepInfo[2]["党员推荐情况"],
		"{17}":  stepInfo[2]["群团组织推优情况"],
		"{18}":  stepInfo[2]["支部确定入党积极分子时间"],
		"{19}":  stepInfo[3]["党委备案时间"],
		"{20}":  stepInfo[4]["培养联系人1"],
		"{21}":  stepInfo[4]["单位及职位1"],
		"{22}":  stepInfo[4]["培养联系人2"],
		"{23}":  stepInfo[4]["单位及职位2"],
		"{24}":  stepInfo[5]["书面思想汇报时间1"],
		"{25}":  stepInfo[5]["书面思想汇报时间2"],
		"{26}":  stepInfo[5]["书面思想汇报时间3"],
		"{27}":  stepInfo[5]["书面思想汇报时间4"],
		"{28}":  stepInfo[5]["支部考察时间1"],
		"{29}":  stepInfo[5]["支部考察时间2"],
		"{30}":  stepInfo[6]["支部确定发展对象人选时间"],
		"{31}":  stepInfo[6]["公示起止时间"],
		"{31a}": endTimeAddOne31,
		"{32}":  stepInfo[6]["党总支审议时间"],
		"{33}":  stepInfo[7]["党委备案同意时间"],
		"{34}":  stepInfo[8]["入党介绍人1"],
		"{35}":  stepInfo[8]["单位及职务1"],
		"{36}":  stepInfo[8]["入党介绍人2"],
		"{37}":  stepInfo[8]["单位及职务2"],
		"{38}":  stepInfo[9]["政审是否合格"],
		"{39}":  stepInfo[9]["政审人(单位、职务、签名)"],
		"{40}":  stepInfo[10]["参加发展对象集中培训起止时间"],
		"{41}":  stepInfo[10]["培训成绩"],
		"{42}":  stepInfo[11]["支部审查时间"],
		"{43}":  stepInfo[12]["党委预审时间"],
		"{44}":  stepInfo[13]["入党志愿书编号"],
		"{45}":  stepInfo[14]["支部大会会议时间"],
		"{46}":  stepInfo[14]["有表决权党员人数"],
		"{47}":  stepInfo[14]["应到人数"],
		"{48}":  stepInfo[14]["实到人数"],
		"{49}":  stepInfo[14]["赞成票数"],
		"{50}":  stepInfo[14]["反对票数"],
		"{51}":  stepInfo[14]["弃权票数"],
		"{52}":  stepInfo[15]["党委派人谈话时间"],
		"{53}":  stepInfo[15]["谈话人(单位、职务、签名)"],
		"{54}":  stepInfo[16]["党委审批时间"],
		"{55}":  stepInfo[16]["党委委员人数"],
		"{56}":  stepInfo[16]["实到人数"],
		"{57}":  stepInfo[16]["同意人数"],
		"{58}":  stepInfo[16]["预备期起止时间"],
		"{59}":  stepInfo[17]["向上级党委组织部门备案时间"],
		"{60}":  stepInfo[18]["编入党支部名称"],
		"{61}":  stepInfo[19]["入党宣誓时间"],
		"{62}":  stepInfo[21]["申请转正时间"],
		"{63}":  stepInfo[21]["转正公示起止时间"],
		"{64}":  stepInfo[22]["支部大会会议时间"],
		"{65}":  stepInfo[22]["有表决权党员人数"],
		"{66}":  stepInfo[22]["应到人数"],
		"{67}":  stepInfo[22]["实到人数"],
		"{68}":  stepInfo[22]["赞成票数"],
		"{69}":  stepInfo[22]["反对票数"],
		"{70}":  stepInfo[22]["弃权票数"],
		"{71}":  stepInfo[22]["会议决议"],
		"{72}":  stepInfo[23]["党委审批时间"],
		"{73}":  stepInfo[23]["党委委员人数"],
		"{74}":  stepInfo[23]["实到人数"],
		"{75}":  stepInfo[23]["同意人数"],
		"{76}":  stepInfo[23]["党龄起算时间"],
		"{77}":  stepInfo[23]["档案整理人(签名)"],
		"{153}": strings.Split(stepInfo[0]["入党申请书接收人(单位、职务、签名)"], " ")[2],
		"{a1}":  stepInfo[2]["会议记录人"],
		"{a2}":  stepInfo[2]["会议地点"],
		"{a3}":  stepInfo[2]["会议主持人"],
	}

	//判断培养联系人是否被修改
	checkContacts(replacements, contact.Id, 7)
	//判断人党介绍人是否被修改
	checkSponsor(replacements, contact.Id, 7)
	//判断所属党支部是否被修改
	checkOrg(replacements, contact.Id, 7)

	//积极分子
	url, err := contact.FillTemplate("user1.docx", "Development", replacements)
	if err != nil {
		log.Printf("Error filling template: %v", err)
	}
	word := "Development" + user.Account + define.WORD_NAME
	pdf := "Development" + user.Account + define.PDF_NAME
	ConvertDocxToPDF(word)
	ALiYunOssPut(pdf)
	defer DelFile("user1.docx")
	defer DelFile(word)
	return pdf, url
}
func WordFill2(user models.UserResponse) (string, string) {
	ALiYunOssGet("user2.docx")
	contact := &Contract{}
	copier.Copy(&contact, &user)
	contact.Birthday = user.Birthday
	//获取步骤详情
	stepInfo, err := ParseStep(contact.Id)
	if err != nil {
		return "", ""
	}
	step0 := fmt.Sprintf("%s，%s，%s族，%s文化,%s，%s出生", contact.Name, contact.Sex, contact.Nation, contact.Education, contact.Domicile, contact.Birthday)
	// 替换占位符
	replacements := map[string]string{

		"{1}":   contact.Organization,
		"{2}":   contact.Name,
		"{3}":   contact.Sex,
		"{4}":   contact.Nation,
		"{5}":   contact.Birthday,
		"{6}":   contact.Origin,
		"{7}":   contact.Occupation,
		"{10}":  contact.Education,
		"{8}":   contact.IdentityId,
		"{9}":   contact.Address,
		"{11}":  contact.Phone,
		"(80)":  step0,
		"{12}":  stepInfo[0]["入党申请时间"],
		"{13}":  stepInfo[0]["入党申请书接收人(单位、职务、签名)"],
		"{14}":  stepInfo[1]["支部谈话时间"],
		"{15}":  stepInfo[1]["谈话人(单位、职务、签名)"],
		"{16}":  stepInfo[2]["党员推荐情况"],
		"{17}":  stepInfo[2]["群团组织推优情况"],
		"{18}":  stepInfo[2]["支部确定入党积极分子时间"],
		"{19}":  stepInfo[3]["党委备案时间"],
		"{20}":  stepInfo[4]["培养联系人1"],
		"{21}":  stepInfo[4]["单位及职位1"],
		"{22}":  stepInfo[4]["培养联系人2"],
		"{23}":  stepInfo[4]["单位及职位2"],
		"{24}":  stepInfo[5]["书面思想汇报时间1"],
		"{25}":  stepInfo[5]["书面思想汇报时间2"],
		"{26}":  stepInfo[5]["书面思想汇报时间3"],
		"{27}":  stepInfo[5]["书面思想汇报时间4"],
		"{28}":  stepInfo[5]["支部考察时间1"],
		"{29}":  stepInfo[5]["支部考察时间2"],
		"{30}":  stepInfo[6]["支部确定发展对象人选时间"],
		"{31}":  stepInfo[6]["公示起止时间"],
		"{32}":  stepInfo[6]["党总支审议时间"],
		"{33}":  stepInfo[7]["党委备案同意时间"],
		"{34}":  stepInfo[8]["入党介绍人1"],
		"{35}":  stepInfo[8]["单位及职务1"],
		"{36}":  stepInfo[8]["入党介绍人2"],
		"{37}":  stepInfo[8]["单位及职务2"],
		"{38}":  stepInfo[9]["政审是否合格"],
		"{39}":  stepInfo[9]["政审人(单位、职务、签名)"],
		"{40}":  stepInfo[10]["参加发展对象集中培训起止时间"],
		"{41}":  stepInfo[10]["培训成绩"],
		"{42}":  stepInfo[11]["支部审查时间"],
		"{43}":  stepInfo[12]["党委预审时间"],
		"{44}":  stepInfo[13]["入党志愿书编号"],
		"{45}":  stepInfo[14]["支部大会会议时间"],
		"{46}":  stepInfo[14]["有表决权党员人数"],
		"{47}":  stepInfo[14]["应到人数"],
		"{48}":  stepInfo[14]["实到人数"],
		"{49}":  stepInfo[14]["赞成票数"],
		"{50}":  stepInfo[14]["反对票数"],
		"{51}":  stepInfo[14]["弃权票数"],
		"{52}":  stepInfo[15]["党委派人谈话时间"],
		"{53}":  stepInfo[15]["谈话人(单位、职务、签名)"],
		"{54}":  stepInfo[16]["党委审批时间"],
		"{55}":  stepInfo[16]["党委委员人数"],
		"{56}":  stepInfo[16]["实到人数"],
		"{57}":  stepInfo[16]["同意人数"],
		"{58}":  stepInfo[16]["预备期起止时间"],
		"{580}": EndSplit(stepInfo[16]["预备期起止时间"]),
		"{581}": StartAndSubSeven(stepInfo[16]["预备期起止时间"]),
		"{59}":  stepInfo[17]["向上级党委组织部门备案时间"],
		"{60}":  stepInfo[18]["编入党支部名称"],
		"{61}":  stepInfo[19]["入党宣誓时间"],
		"{62}":  stepInfo[21]["申请转正时间"],
		"{63}":  stepInfo[21]["转正公示起止时间"],
		"{63a}": StartAndSubOne(stepInfo[21]["转正公示起止时间"]),
		"{63b}": EndAndAddOne(stepInfo[21]["转正公示起止时间"]),
		"{64}":  stepInfo[22]["支部大会会议时间"],
		"{65}":  stepInfo[22]["有表决权党员人数"],
		"{66}":  stepInfo[22]["应到人数"],
		"{67}":  stepInfo[22]["实到人数"],
		"{68}":  stepInfo[22]["赞成票数"],
		"{69}":  stepInfo[22]["反对票数"],
		"{70}":  stepInfo[22]["弃权票数"],
		"{71}":  stepInfo[22]["会议决议"],
		"{72}":  stepInfo[23]["党委审批时间"],
		"{73}":  stepInfo[23]["党委委员人数"],
		"{74}":  stepInfo[23]["实到人数"],
		"{75}":  stepInfo[23]["同意人数"],
		"{76}":  stepInfo[23]["党龄起算时间"],
		"{77}":  stepInfo[23]["档案整理人(签名)"],
		"{153}": strings.Split(stepInfo[0]["入党申请书接收人(单位、职务、签名)"], " ")[2],
		"{a1}":  stepInfo[2]["会议记录人"],
		"{a2}":  stepInfo[2]["会议地点"],
		"{a3}":  stepInfo[2]["会议主持人"],
	}
	fmt.Println(replacements["(80)"])
	//判断培养联系人是否被修改
	checkContacts(replacements, contact.Id, 12)
	//判断人党介绍人是否被修改
	checkSponsor(replacements, contact.Id, 12)
	//判断所属党支部是否被修改
	checkOrg(replacements, contact.Id, 12)
	//预备党员
	url, err := contact.FillTemplate("user2.docx", "Prepare", replacements)
	if err != nil {
		log.Printf("Error filling template: %v", err)
	}
	word := "Prepare" + user.Account + define.WORD_NAME
	pdf := "Prepare" + user.Account + define.PDF_NAME
	ConvertDocxToPDF(word)
	ALiYunOssPut(pdf)
	defer DelFile("user2.docx")
	defer DelFile(word)
	return pdf, url
}
func WordFillByExcel(user models.UserExcel) (string, string) {
	ALiYunOssGet("excel.docx")
	replacements := map[string]string{
		"{189}": user.College,
		"{187}": user.Enrollment,
		"{1}":   user.Class,
		"{2}":   user.Class,
		"{3}":   user.Major,
		"{4}":   user.Name,
		"{5}":   user.Occupation,
		"{6}":   models.GenderMap[user.Sex],
		"{7}":   user.Nation,
		"{11}":  user.IdentityId,
		"{8}":   user.Birthday,
		"{9}":   user.Origin,
		"{210}": user.BirthPlace,
		"{177}": user.Origin,
		"{168}": user.Political,
		"{178}": user.Title,
		"{212}": user.Dorm,
		"{13}":  user.Phone,
		"{172}": user.Education,
		"{14}":  user.Degree,
		"{12}":  user.Address,
	}
	contact := &Contract{
		Account: user.Account,
	}
	url, err := contact.FillTemplate("excel.docx", "Active", replacements)
	if err != nil {
		log.Printf("Error filling template: %v", err)
	}
	word := "Active" + user.Account + define.EXCEL_WORD
	pdf := "Active" + user.Account + define.EXCEL_PDF
	ConvertDocxToPDF(word)
	ALiYunOssPut(pdf)
	defer DelFile("excel.docx")
	defer DelFile(word)
	return pdf, url
}
func ParseStep(id int) ([]map[string]string, error) {
	var stepId int
	err := models.DB.Model(models.User{}).Select("step").Where("id=?", id).Scan(&stepId).Error
	if err != nil {
		return nil, err
	}
	var data []map[string]string
	var str []string
	err = models.DB.Model(models.Steps{}).Select("data").Where("user_id=? ", id).Order("step_id ASC").Find(&str).Error
	if err != nil {
		return nil, err
	}
	for _, result := range str {
		toMap, _ := jsonToMap(result)
		data = append(data, toMap)
	}
	return data, nil
}
func jsonToMap(jsonStr string) (map[string]string, error) {
	var result map[string]interface{}
	err := json.Unmarshal([]byte(jsonStr), &result)
	if err != nil {
		return nil, fmt.Errorf("JSON解析失败: %v", err)
	}

	strMap := make(map[string]string)
	for k, v := range result {
		switch v := v.(type) {
		case string:
			strMap[k] = v
		case nil:
			strMap[k] = ""
		default:
			// 如果不是string类型，转换为字符串表示
			strMap[k] = fmt.Sprintf("%v", v)
		}
	}

	return strMap, nil
}
func checkOrg(replacements map[string]string, id int, sid int) {
	//使用旧的原来的党支部
	redisOrgKey := fmt.Sprintf("OrderOrganization:%d", id)
	//从redis中获取
	org, _ := models.RDB.Get(context.Background(), redisOrgKey).Result()
	if org != "" {
		cacheKeyStep := fmt.Sprintf("OrgStep:%d", id)
		s, _ := models.RDB.Get(context.Background(), cacheKeyStep).Result()
		step, _ := strconv.Atoi(s)
		//如果在积极分子之后改的就使用旧的
		if step >= sid {
			replacements["{1}"] = org
		}
	}

}
func checkContacts(replacements map[string]string, id int, sid int) {
	cacheKey := fmt.Sprintf("Contacts:%d", id)
	result, _ := models.RDB.Get(context.Background(), cacheKey).Result()
	if result != "" {
		cacheKeyStep := fmt.Sprintf("ContactsStep:%d", id)
		s, _ := models.RDB.Get(context.Background(), cacheKeyStep).Result()
		step, _ := strconv.Atoi(s)
		if step <= sid {
			toMap, _ := jsonToMap(result)
			replacements["{20}"] = toMap["培养联系人1"]
			replacements["{21}"] = toMap["单位及职位1"]
			replacements["{22}"] = toMap["培养联系人2"]
			replacements["{23}"] = toMap["单位及职位2"]
		}
	}

}
func checkSponsor(replacements map[string]string, id int, sid int) {
	cacheKey := fmt.Sprintf("Sponsor:%d", id)
	result, _ := models.RDB.Get(context.Background(), cacheKey).Result()
	if result != "" {
		cacheKeyStep := fmt.Sprintf("SponsorStep:%d", id)
		s, _ := models.RDB.Get(context.Background(), cacheKeyStep).Result()
		step, _ := strconv.Atoi(s)
		if step <= sid {
			toMap, _ := jsonToMap(result)
			replacements["{34}"] = toMap["入党介绍人1"]
			replacements["{35}"] = toMap["单位及职位1"]
			replacements["{36}"] = toMap["入党介绍人2"]
			replacements["{37}"] = toMap["单位及职位2"]
		}
	}
}
